class Profile {
    constructor() {
        this.form = document.querySelector('#profileForm');
        this.username = document.querySelector('#username');
        this.age = document.querySelector('#age');
        this.gender = document.querySelector('#gender');
        this.nickname = document.querySelector('#nickname');
        
        this.init();
    }

    init() {
        this.checkLoginStatus();
        this.loadUserInfo();
        this.bindEvents();
    }

    // 检查登录状态
    checkLoginStatus() {
        const token = localStorage.getItem('token');
        if (!token) {
            alert('请先登录！');
            window.location.href = 'login.html';
        }
    }

    // 加载用户信息
    async loadUserInfo() {
        const token = localStorage.getItem('token');
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');

        try {
            const response = await fetch(`http://localhost:8888/users/info/${userInfo.id}`, {
                headers: {
                    'authorization': token
                }
            });

            const data = await response.json();
            
            if (data.code === 1) {
                // 填充表单
                this.username.value = data.info.username || '';
                this.age.value = data.info.age || '';
                this.gender.value = data.info.gender || '';
                this.nickname.value = data.info.nickname || '';
            } else {
                alert(data.message || '获取用户信息失败');
            }
        } catch (error) {
            alert('获取用户信息失败，请检查网络连接');
            console.error('获取用户信息错误：', error);
        }
    }

    bindEvents() {
        this.form.addEventListener('submit', (e) => {
            e.preventDefault();
            this.handleSubmit();
        });
    }

    // 处理表单提交
    async handleSubmit() {
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
        const token = localStorage.getItem('token');

        try {
            const response = await fetch('http://localhost:8888/users/update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'authorization': token
                },
                body: new URLSearchParams({
                    id: userInfo.id,
                    age: this.age.value,
                    gender: this.gender.value,
                    nickname: this.nickname.value
                })
            });

            const data = await response.json();
            
            if (data.code === 1) {
                alert('修改成功！');
                // 更新本地存储的用户信息
                userInfo.nickname = this.nickname.value;
                localStorage.setItem('userInfo', JSON.stringify(userInfo));
            } else {
                alert(data.message || '修改失败，请重试');
            }
        } catch (error) {
            alert('修改失败，请检查网络连接');
            console.error('修改用户信息错误：', error);
        }
    }
}

// 初始化个人中心功能
document.addEventListener('DOMContentLoaded', () => {
    new Profile();
}); 